package com.newlink.building.userinfo.activity

import android.content.Intent
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.newlink.building.common_base.activities.NL_WebViewActivityNL
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityHelpBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_HelpPresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_HelpContract

/**
 * @Author: Jake
 * @Date: 2023-09-12
 * @Description:
 */
class Module_UserInfo_HelpActivityNL : NL_BaseMvpActivity<Module_UserInfo_HelpContract.View, Module_UserInfo_HelpContract.Presenter>(),
    Module_UserInfo_HelpContract.View, View.OnClickListener {

    private var titleTextView: TextView? = null

    override fun createPresenter(): Module_UserInfo_HelpContract.Presenter = Module_UserInfo_HelpPresenter()

//    override fun attachLayoutRes(): Int = R.layout.activity_help

    private lateinit var mBinding: ActivityHelpBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityHelpBinding.inflate(layoutInflater)
        return mBinding.root
    }

    // supervisor
    override fun initData() {
//        mPresenter?.getFAQ()
    }

    override fun initView() {
        super.initView()
        val url = intent.getStringExtra("link")
        titleTextView = findViewById<TextView>(R.id.tv_title_bar_center)
        titleTextView?.text = resources.getText(R.string.help_and_feedback)

        findViewById<View>(R.id.ll_title_bar_back).setOnClickListener {
            if (mBinding.helpWebView.canGoBack()) {
                mBinding.helpWebView.goBack() // 如果 WebView 有历史记录，则返回上一页面
            } else {
                finish()
            }
        }
        mBinding.titleBar.tvTitleBarRight.visibility = View.VISIBLE
        mBinding.titleBar.tvTitleBarRight.text = getString(R.string.settings_help_feedback)
        mBinding.titleBar.rlTitleBarRight.setOnClickListener {
            startActivity(Intent(this, Module_UserInfo_FeedbackActivityNL::class.java))
        }

        mBinding.helpWebView.apply {
            settings.javaScriptEnabled = true
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    view?.loadUrl(request?.url.toString())
                    return true
                }

                override fun onPageFinished(view: WebView, url: String) {
                    super.onPageFinished(view, url)
                    var titleBarRightVisible = false
                    if (url.contains("createApp")) {
                        titleTextView?.text = resources.getText(R.string.help_activity_tips_account)
                    } else if (url.contains("operations")) {
                        titleTextView?.text = resources.getText(R.string.help_activity_tips_account)
                    } else if (url.contains("temporaryPassword")) {
                        titleTextView?.text = resources.getText(R.string.help_activity_tips_account)
                    } else if (url.contains("forgotPassword")) {
                        titleTextView?.text = resources.getText(R.string.help_activity_tips_account)
                    } else if (url.contains("UseBluetooth")) {
                        titleTextView?.text = resources.getText(R.string.help_activity_tips_account)
                    } else {
                        titleBarRightVisible = true
                        titleTextView?.text = resources.getText(R.string.help_and_feedback)
                    }
                    mBinding.titleBar.tvTitleBarRight.visibility =
                        if (titleBarRightVisible) View.VISIBLE else View.GONE
                }

            }
//            loadUrl(if (LocaleUtils.isSystemLocaleZh()) Constant.HELP_URL else Constant.HELP_URL_EN)

        }
        url?.let {
            mBinding.helpWebView.loadUrl(it)
        }


//        mBinding.createFamilyItem.setOnClickListener {
//            loadWebView(
//                getString(R.string.help_activity_tips_account),
//                if (LocaleUtils.isSystemLocaleZh()) Constant.HELP_CREATEAPP else Constant.HELP_CREATEAPP_EN
//            )
//        }
//
//        mBinding.appOperation.setOnClickListener {
//            loadWebView(
//                getString(R.string.help_activity_tips_operation),
//                if (LocaleUtils.isSystemLocaleZh()) Constant.HELP_OPERATIONS else Constant.HELP_OPERATIONS_EN
//            )
//        }
//
//        mBinding.tempPassword.setOnClickListener {
//            loadWebView(
//                getString(R.string.help_activity_tips_temp_password),
//                if (LocaleUtils.isSystemLocaleZh()) Constant.HELP_TEMPORARYPASSWORD else Constant.HELP_TEMPORARYPASSWORD_EN
//            )
//        }
//
//        mBinding.findPassword.setOnClickListener {
//            loadWebView(
//                getString(R.string.help_activity_tips_forget_password),
//                if (LocaleUtils.isSystemLocaleZh()) Constant.HELP_FORGOTPASSWORD else Constant.HELP_FORGOTPASSWORD_EN
//            )
//        }
    }

    // 覆盖 onBackPressed 方法
    override fun onBackPressed() {
        if (mBinding.helpWebView.canGoBack()) {
            mBinding.helpWebView.goBack() // 如果 WebView 有历史记录，则返回上一页面
        } else {
            super.onBackPressed() // 否则，执行默认的返回操作（退出应用）
        }
    }

    private fun loadWebView(title: String, url: String) {
        if (url.isNotEmpty() && title.isNotEmpty()) {
            NL_WebViewActivityNL.startSelf(this@Module_UserInfo_HelpActivityNL, url, title)
        }
    }

    override fun showData() {
    }

    override fun onGetFAQSuccess(url: String?) {
        if (!url.isNullOrEmpty()) {
            mBinding.helpWebView.loadUrl(url)
        }
    }

    override fun onClick(v: View?) {
    }
}